﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using Google.GData;
using Google.GData.Extensions;
using Google.GData.Calendar;
using SuperCalendar.DataAccess;
using SuperCalendar.Models;

namespace SuperCalendar.Services
{
    public static class LocalCalendarService
    {
        public static IEnumerable<Event> GeEvents()
        {
            return Events.GetAllEvents();
        }
        public static void Insert(Event ev)
        {
            Events.Insert(ev);
            Events.Save();
        }

        public static void Delete(Event ev)
        {
            Events.Delete(ev);
            Events.Save();
        }
        public static void Synchronize()
        {
            Synchronize(null, null);
        }
        public static void Synchronize(Nullable<DateTime> startTime, Nullable<DateTime> endTime)
        {
            String username = ConfigurationSettings.AppSettings["UserName"];
            String password = ConfigurationSettings.AppSettings["Password"];
            String version = ConfigurationSettings.AppSettings["Version"];
            if (!username.ToLower().Trim().Contains("@gmail.com"))
                username += "@gmail.com";

            Google.GData.Calendar.CalendarService gCalendarService = new Google.GData.Calendar.CalendarService(version);
            gCalendarService.setUserCredentials(username, password);
            EventQuery query = new EventQuery();
            if (startTime != null && endTime != null)
            {
                query.StartDate = startTime.Value;
                query.EndTime = endTime.Value;
            }
            query.Uri = new Uri("http://www.google.com/calendar/feeds/" + username + "/private/full");
            EventFeed eventFeed = gCalendarService.Query(query);
            List<Event> serverNewEvents = new List<Event>();

            foreach (EventEntry entry in eventFeed.Entries)
            {
                Event ev = Events.GetAllEvents().SingleOrDefault(v => entry.GoogleId().Equals(v.GoogleId));
                if (ev == null)
                {
                    Event newEvent = entry.ToEvent();
                    serverNewEvents.Add(newEvent);
                }
                else
                {
                    if (ev.IsDeleted)
                    {
                        entry.Delete();
                        Events.Delete(ev);
                    }
                    else
                    {
                        if (ev.Updated > entry.Updated)
                        {
                            entry.FromEvent(ev);
                            entry.Update();
                        }
                        else
                        {
                            Event newEvent = entry.ToEvent();
                            newEvent.Id = ev.Id;
                            Events.Update(newEvent);
                        }
                    }

                }
            }

            List<Event> clientNewEvents = (from e in Events.GetAllEvents() where e.IsNew == true select e).ToList();
            Uri postUri = new Uri("http://www.google.com/calendar/feeds/default/private/full");

            foreach (Event e in clientNewEvents)
            {
                EventEntry entry = new EventEntry();
                entry.FromEvent(e);
                entry = gCalendarService.Insert(postUri, entry);
                e.GoogleId = entry.GoogleId();
                e.IsNew = false;
            }

            Events.InsertRange(serverNewEvents);

            Events.Save();
        }
    }
}
